package net.chathu.airgw.packets.chat
{
	import net.chathu.airgw.Config;
	import net.chathu.airgw.model.JoinedRoomInfo;
	import net.chathu.airgw.model.PrivateChatInfo;
	import net.chathu.airgw.model.User;
	import net.chathu.airgw.packets.gw.serverpackets.NoticePacket;
	import net.chathu.airgw.packets.gw.serverpackets.PartPacket;
	import net.chathu.airgw.packets.gw.serverpackets.QuitPacket;
	
	/**
	 * ...
	 * @author ze
	 */
	public class IRemUsers extends ChatPacket
	{
		override public function execute():void
		{
			var userId:int = parseInt(data[2]);
			var user:User = User(sessionData.chatData.users.getUser(userId));
			if (!user)
			{
				trace("Ismeretlen user lepett ki: " + userId + " (IRemUser");
				trace("A torles megszakitva!");
				return;
			}
			
			
			
			var sameJoinedRooms:Array = sessionData.chatData.getCommonJoinedRoomsWithUser(userId);
			
			if (sameJoinedRooms.length > 0 || sessionData.chatData.isInUsersRoom)
			{
				//legalabb 1 kozos szoba volt
				for each (var roomInfo:JoinedRoomInfo in sameJoinedRooms)
				{
					roomInfo.removeUser(userId);
				}
				sendToClient(new QuitPacket(user.userHost, ""));
			}
			/* else {
			   //ha nincs kozos szoba, nincs ertesites
			 }*/
			
			//TODO: privat uzenetekbol is torolni majd!
			
			var privInfo:PrivateChatInfo = sessionData.chatData.getPrivateChatInfoByPartnerId(user.id);
			if (privInfo)
			{
				//epp beszelgettetek...
				sessionData.chatData.privateMessageQueue.removePartnerMessages(userId); //toroljuk, anelkul hogy barmit is csinalnank vele
				sendToClient(new NoticePacket(user.userHost, sessionData.nick, user.name + " kilépett!"));
				sessionData.chatData.removePrivateChatInfo(privInfo.privateId);
			}
			
			//Vegul: user torles a felhasznaloi listabol
			sessionData.chatData.users.remove(user);
		}
	
	}

}